<template>
  <div class="app-container">
    <el-form v-show="showSearch" ref="queryForm" :model="queryParams" :inline="true" label-width="68px">
      <el-form-item label="用户名" prop="userName">
        <el-input
          v-model="queryParams.userName"
          placeholder="请输入用户名"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="手机号码" prop="phonenumber">
        <el-input
          v-model="queryParams.phonenumber"
          placeholder="请输入手机号码"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          v-hasPermi="['community:member:add']"
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
        >添加成员</el-button>
      </el-col>
      <!--      <el-col :span="1.5">-->
      <!--        <el-button-->
      <!--          type="success"-->
      <!--          plain-->
      <!--          icon="el-icon-edit"-->
      <!--          size="mini"-->
      <!--          :disabled="single"-->
      <!--          @click="handleUpdate"-->
      <!--          v-hasPermi="['community:member:edit']"-->
      <!--        >修改</el-button>-->
      <!--      </el-col>-->
      <el-col :span="1.5">
        <el-button
          v-hasPermi="['community:member:remove']"
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
        >移除成员</el-button>
      </el-col>
      <!--      <el-col :span="1.5">-->
      <!--        <el-button-->
      <!--          type="warning"-->
      <!--          plain-->
      <!--          icon="el-icon-download"-->
      <!--          size="mini"-->
      <!--          @click="handleExport"-->
      <!--          v-hasPermi="['community:member:export']"-->
      <!--        >导出</el-button>-->
      <!--      </el-col>-->
      <el-col :span="6">
        <span style="font-size: large; font-weight: bolder; ">当前群组：{{ currentPageGroupName }}</span>
      </el-col>
      <right-toolbar :show-search.sync="showSearch" @queryTable="getList" />
    </el-row>

    <el-table v-loading="loading" :data="memberList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="用户编号" align="center" prop="user.userId" />
      <el-table-column label="用户名称" align="center" prop="user.userName" />
      <el-table-column label="用户昵称" align="center" prop="user.nickName" />
      <el-table-column label="部门" align="center" prop="user.dept.deptName" />
      <el-table-column label="手机号码" align="center" prop="user.phonenumber" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            v-hasPermi="['community:member:remove']"
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
          >移除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改群组成员列表对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="700px" append-to-body>
      <el-form ref="userForm" :model="userQueryParams" label-width="68px" :inline="true">
        <el-form-item label="用户名" prop="userName">
          <el-input
            v-model="userQueryParams.userName"
            clearable
            size="mini"
            @keyup.enter.native="handleUserQuery"
          />
        </el-form-item>
        <el-form-item label="手机号码" prop="phonenumber">
          <el-input
            v-model="userQueryParams.phonenumber"
            clearable
            size="mini"
            @keyup.enter.native="handleUserQuery"
          />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" icon="el-icon-search" size="mini" circle @click="handleUserQuery" />
          <!--          <el-button type="primary" icon="el-icon-search" size="mini" @click="handleUserQuery">搜索</el-button>-->
        </el-form-item>
      </el-form>
      <!--      <div>{{userList}}</div>-->
      <el-table v-loading="loading" :data="userList" max-height="250">
        <!--        <el-table-column type="selection" width="55" align="center" />-->
        <!--        <el-table-column label="用户编号" align="center" prop="userId" />-->
        <el-table-column label="用户名称" align="center" prop="userName" />
        <el-table-column label="用户昵称" align="center" prop="nickName" />
        <el-table-column label="部门" align="center" prop="dept.deptName" />
        <el-table-column label="手机号码" align="center" prop="phonenumber" />
        <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
          <template slot-scope="scope">
            <el-button
              v-if="isInGroup(scope.row.userId, memberList)"
              size="mini"
              type="text"
              style="color: gray"
              disabled
            >已添加</el-button>
            <el-button
              v-else
              v-hasPermi="['community:member:add']"
              size="mini"
              type="text"
              @click="handleAddMember(scope.row)"
            >添加</el-button>
          </template>
        </el-table-column>
      </el-table>
      <div slot="footer" class="dialog-footer">
        <!--        <el-button type="primary" @click="submitForm">确 定</el-button>-->
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
// import { listMember, getMember, delMember, addMember, updateMember, exportMember } from "@/api/owner/member";
import { listMember, delMember, addMember } from '@/api/owner/member'
import { getGroup } from '@/api/owner/group'
import { listUser } from '@/api/system/user'

export default {
  name: 'Member',
  components: {
  },
  data() {
    return {
      // 当前所在群组名
      currentPageGroupName: null,
      // 当前管理员社区ID
      currentCommunityID: null,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 群组成员列表表格数据
      memberList: [],
      // 用户检索结果表格数据
      userList: [],
      // 弹出层标题
      title: '',
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        groupId: null,
        userName: null,
        phonenumber: null
      },
      // 用户查询参数
      userQueryParams: {
        // pageNum: 1,
        // pageSize: 10,
        userName: null,
        phonenumber: null,
        // communityId: null
      },
      // 表单参数
      form: {},
      // 新增成员隐藏表单参数
      userForm: {
        groupId: null,
        userId: null
      },
      // 表单校验
      rules: {
        userId: [
          { required: true, message: '用户ID不能为空', trigger: 'blur' }
        ],
        groupId: [
          { required: true, message: '群组ID不能为空', trigger: 'blur' }
        ]
      }
    }
  },
  created() {
    this.queryParams.groupId = this.$route.params.id
    this.userForm.groupId = this.$route.params.id
    getGroup(this.queryParams.groupId).then(response => {
      // console.log(response.data.groupName)
      this.currentPageGroupName = response.data.groupName
      // this.currentCommunityID = response.data.communityId;
    })
    this.getList()
  },
  methods: {
    // 通过用户ID判断该用户是否在本群组名单中
    isInGroup(id, memberList) {
      for (let i = 0; i < memberList.length; i++) {
        if (id === memberList[i].user.userId) {
          return true
        }
      }
      return false
    },
    /** 查询群组成员列表 */
    getList() {
      this.loading = true
      const tmpList = []
      listMember(this.queryParams).then(response => {
        const mapList = response.rows
        this.total = response.total
        for (let i = 0; i < this.total; i++) {
          // this.memberList.push(mapList[i].ownerGroupMember);
          // this.memberUserList.push(mapList[i].sysUser);
          tmpList.push({
            member: mapList[i].ownerGroupMember,
            user: mapList[i].sysUser
          })
        }
        this.memberList = tmpList
        this.loading = false
        // console.log(this.memberList);
      })
    },
    // 取消按钮
    cancel() {
      this.open = false
      // this.reset();
    },
    // 表单重置
    reset() {
      this.userList = []
      this.resetForm('userForm')
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1
      this.getList()
    },
    /** 搜索用户按钮操作 */
    handleUserQuery() {
      this.loading = true
      // this.userQueryParams.communityId = this.currentCommunityID;
      listUser(this.userQueryParams).then(
        (response) => {
          this.userList = response.rows
          this.loading = false
        }
      )
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.member.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 添加成员按钮操作 */
    handleAdd() {
      this.reset()
      this.open = true
      this.title = '添加群组成员'
    },
    /** dialog添加成员操作 */
    handleAddMember(row) {
      // console.log('执行到这')
      this.userForm.userId = row.userId
      addMember(this.userForm).then(response => {
        this.msgSuccess('添加成功')
        // this.open = false;
        this.getList()
      })
    },
    // /** 修改按钮操作 */
    // handleUpdate(row) {
    //   this.reset();
    //   const id = row.id || this.ids
    //   getMember(id).then(response => {
    //     this.form = response.data;
    //     this.open = true;
    //     this.title = "修改群组成员列表";
    //   });
    // },
    // /** 提交按钮 */
    // submitForm() {
    //   this.$refs["form"].validate(valid => {
    //     if (valid) {
    //       if (this.form.id != null) {
    //         updateMember(this.form).then(response => {
    //           this.msgSuccess("修改成功");
    //           this.open = false;
    //           this.getList();
    //         });
    //       } else {
    //         addMember(this.form).then(response => {
    //           this.msgSuccess("新增成功");
    //           this.open = false;
    //           this.getList();
    //         });
    //       }
    //     }
    //   });
    // },
    /** 删除按钮操作 */
    handleDelete(row) {
      // row不含member，不是table行的移除按钮
      let ids = []
      if (!row.member) {
        ids = this.ids
      } else {
        ids = row.member.id
      }
      // const ids = row.member.id || this.ids;
      // console.log(ids)
      this.$confirm('是否确认删除该群组成员?', '警告', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(function() {
        return delMember(ids)
      }).then(() => {
        this.getList()
        this.msgSuccess('删除成功')
      })
    },
    // /** 导出按钮操作 */
    // handleExport() {
    //   const queryParams = this.queryParams;
    //   this.$confirm('是否确认导出所有群组成员列表数据项?', "警告", {
    //     confirmButtonText: "确定",
    //     cancelButtonText: "取消",
    //     type: "warning"
    //   }).then(function() {
    //     return exportMember(queryParams);
    //   }).then(response => {
    //     this.download(response.msg);
    //   })
    // }
  }
}
</script>
